package com.niit.bigdata.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.niit.bigdata.entity.Order;
import com.niit.bigdata.service.RedisService;
import com.niit.bigdata.service.SparkSqlService;
import com.niit.bigdata.util.ResponseBuilder;
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@RestController
@RequestMapping("/kafka")
public class OrderController {

    private final RedisService redisService;
    private final ResponseBuilder responseBuilder;
    private final ObjectMapper objectMapper;

    public OrderController(RedisService redisService, ResponseBuilder responseBuilder, ObjectMapper objectMapper) {
        this.redisService = redisService;
        this.responseBuilder = responseBuilder;
        this.objectMapper = objectMapper;
    }

    @GetMapping("getCurrentValidnum")
    public void getCurrentValidnum(HttpServletResponse response) throws IOException {
        try {
            String data = redisService.getString("valid_num");
            data = data.replace("\"", "");
            String[] orderInfoArray = data.split(", ");
            int validOrderCount = 0;
            int invalidOrderCount = 0;
            for (String order : orderInfoArray) {
                if (order.startsWith("有效订单总数:")) {
                    validOrderCount = Integer.parseInt(order.substring(7));
                } else if (order.startsWith("无效订单总数:")) {
                    invalidOrderCount = Integer.parseInt(order.substring(7));
                }
            }
            Map<String, Object> responseData = new HashMap<>();
            responseData.put("validOrderCount", validOrderCount);
            responseData.put("invalidOrderCount", invalidOrderCount);

            responseBuilder.buildJsonResponse(response,responseData, 200, "数据获取成功");


        } catch (IOException e) {
            e.printStackTrace();
            responseBuilder.buildJsonResponse(response, null, 500, "数据处理失败");
        }
    }
//    each_category_valid_num
    @GetMapping("category")
    public void getCategoryValidnum(HttpServletResponse response) throws IOException {
        try {
            String data = redisService.getString("order_counts");
            data = data.replace("\"", "");
            String[] categoryDataArray = data.replaceAll("\\[|\\]", "").split(",");

            // Create a list to store Map objects
            List<Map<String, Integer>> mapList = new ArrayList<>();


            // Iterate through the array and convert each element to a Map
            for (String categoryData : categoryDataArray) {
                try {
                    // Extract category and quantity from each element
                    String[] keyValue = categoryData.split(":");
                    String category = keyValue[0].trim();
                    int quantity = Integer.parseInt(keyValue[1].replaceAll("\\D", "").trim());

                    Map<String, Integer> dataMap = new HashMap<>();
                    dataMap.put(category, quantity);
                    mapList.add(dataMap);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }

                responseBuilder.buildJsonResponse(response, mapList, 200, "数据获取成功");


        } catch (IOException e) {
            e.printStackTrace();
            responseBuilder.buildJsonResponse(response, null, 500, "数据处理失败");
        }
    }

    @GetMapping("categorytotal")
    public void getCategoryTotal(HttpServletResponse response) throws IOException {
        try {
            SparkSqlService sparkSqlService = new SparkSqlService();
            List<com.niit.bigdata.entity.Spark> list = sparkSqlService. GetCategoryList();
            responseBuilder.buildJsonResponse(response, list, 200, "数据获取成功");
        } catch (IOException e) {
            e.printStackTrace();
            responseBuilder.buildJsonResponse(response, null, 500, "数据处理失败");
        }
    }

    @GetMapping("getTotal")
    public void getTotal(HttpServletResponse response) throws IOException {
        try {
            int total = (int) redisService.get("total");
            responseBuilder.buildJsonResponse(response, total, 200, "数据获取成功");
        } catch (IOException e) {
            e.printStackTrace();
            responseBuilder.buildJsonResponse(response, null, 500, "数据处理失败");
        }
    }

    @GetMapping("getOrder")
    public void getOrder(HttpServletResponse response) throws IOException {
        try {
            String data = redisService.getString("order");
            responseBuilder.buildJsonResponse(response, data, 200, "数据获取成功");
        } catch (IOException e) {
            e.printStackTrace();
            responseBuilder.buildJsonResponse(response, null, 500, "数据处理失败");
        }
    }

}